import React from 'react';
import store from './store'
import { SUB_COUNT } from './store/actionType'
export default class Sub extends React.Component {

  constructor (props) {
    super(props)
    this.state = store.getState()
  }

  componentDidMount () {
    store.subscribe(this.storeChange)
  }
  
  storeChange = () => {
    this.setState({
      ...store.getState()
    })
  }

  subCount = () => {
    store.dispatch({
      type: SUB_COUNT,
      value: 2
    })
  }

  render () {
    return (
      <div>
        { this.state.count }
        <button onClick={this.subCount}>-1</button>
      </div>
    )
  }

}